//
//  PingJiaViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "PingJiaViewController.h"

#import "PinJiaCell.h"
#import "StarView.h"
@interface PingJiaViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array;
    NSMutableDictionary *dic;
    
    UIImageView *imageView;
    UILabel *labTit;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation PingJiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setBGColor];
    [self showNav:@"全部评价"];
    array = [NSMutableArray array];
    dic = [NSMutableDictionary dictionary];
    [self loadUI];
    [self loadData];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"available"]];
    imageView.hidden = YES;
    [self.view addSubview:imageView];
    [imageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.centerY.equalTo(self.view.centerY);
    }];
    
    labTit = [[UILabel alloc] init];
    labTit.text = @"暂无评论信息...";
    labTit.font = [UIFont systemFontOfSize:15.0];
    labTit.textColor = [UIColor lightGrayColor];
    labTit.hidden = YES;
    [self.view addSubview:labTit];
    [labTit makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imageView.centerX);
        make.top.equalTo(imageView.bottom).offset(8);
    }];
    
    
}
#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    PinJiaCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PinJiaCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *op = array[indexPath.row];
    cell.labName.text = [op objectForKey:@"nickname"];
    cell.labText.text = [op objectForKey:@"comment"];
    NSString *time = [op objectForKey:@"commenttime"];
    NSString *date = [self getNewDate:time];
    cell.labDate.text = date;
    
    NSString *start = [NSString stringWithFormat:@"%.2f",[[op objectForKey:@"star"] floatValue]];
    CGFloat count = [start floatValue];
    StarView *star = [[StarView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    star.showStar = count * 20;
    [cell.starView addSubview:star];
    
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[op objectForKey:@"avatar"]];
    cell.images.layer.cornerRadius = 11.5;
    cell.images.layer.masksToBounds = YES;
    [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"TIM图片20170810135854"]];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    line.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [view addSubview:line];
    
    NSString *count = [NSString stringWithFormat:@"总评价：%li",[[dic objectForKey:@"count"] integerValue]];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 20, 100, 20)];
    lab.text = count;
    lab.textColor = [UIColor colorWithRed:38/255.0 green:38/255.0 blue:38/255.0 alpha:1.0];
    [view addSubview:lab];
    
    UILabel *labLine = [[UILabel alloc] initWithFrame:CGRectMake(16, 49, ScreenWidth-32, 1)];
    labLine.backgroundColor = COLOR_RGB(217, 217, 217, 1);
    [view addSubview:labLine];
    
    StarView *starView = [[StarView alloc] initWithFrame:CGRectMake(ScreenWidth-80, 20, 80, 15)];
    NSString *counts = [dic objectForKey:@"evaluate"];
    NSInteger index = [counts integerValue];
    starView.showStar = index*20;
    [view addSubview:starView];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50.0;
}
//评论
- (void)loadData{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:_merid forKey:@"merchantid"];
    [parmas setValue:@"1" forKey:@"p"];
    
    [[AFHTTPSessionManager manager] GET:_url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            array = [[responseObject objectForKey:@"result"] objectForKey:@"comment"];
            dic = [[responseObject objectForKey:@"result"] objectForKey:@"commentcount"];
            [_tableView reloadData];
        }else{
            _tableView.hidden = YES;
            imageView.hidden = NO;
            labTit.hidden = NO;
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
